package ru.averta.notes.domain;

import ru.averta.notes.domain.abstr.AbstractPersistentObject;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;

/**
 * <code>GoogleSuggest</code>
 *
 * @author fedor.belov (fedor.belov@mail.ru)
 *         Date: 26.09.11 16:51
 */
@Entity
public class GoogleSuggest extends AbstractPersistentObject {

    @Column
    private String suggestText;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "Suggests_Urls",
            joinColumns =
            @JoinColumn(name = "SUGGEST_ID"),
            inverseJoinColumns =
            @JoinColumn(name = "URL_ID"))
    private Collection<Url> urls = new HashSet<Url>();

    public GoogleSuggest() {
    }

    public GoogleSuggest(String suggestText) {
        this.suggestText = suggestText;
    }

    public void addUrl(Url url) {
        urls.add(url);
    }

    public String getSuggestText() {
        return suggestText;
    }

    public void setSuggestText(String suggestText) {
        this.suggestText = suggestText;
    }

    /**
     * Возвращает true, если данный googleSuggest удовлетворяет введенному пользователем тексту
     * @param text
     * @return
     */
    public boolean isSatisfyText(String text) {
        return (text != null && suggestText.length() >= text.length() && text.equalsIgnoreCase(suggestText.substring(0, text.length())));
    }
}
